<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>欢迎登录 - 人力资源管理系统</h1>
      <el-card shadow="never" class="login-card">
        <!-- 登录表单 -->
        <el-form ref="form" :model="loginForm" :rules="loginRules">

          <!-- 手机号 -->
          <el-form-item prop="mobile">
            <el-input v-model.trim="loginForm.mobile" type="text" placeholder="请输入手机号" clearable>
              <i slot="prefix" class="el-input__icon el-icon-user-solid" />
            </el-input>
          </el-form-item>

          <!-- 密码 -->
          <el-form-item prop="password">
            <el-input v-model.trim="loginForm.password" show-password placeholder="请输入密码">
              <i slot="prefix" class="el-input__icon el-icon-lock" />
            </el-input>
          </el-form-item>

          <!-- 用户协议 -->
          <!-- <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">用户平台使用协议</el-checkbox>
          </el-form-item> -->

          <!-- 登录按钮 -->
          <el-form-item>
            <el-button type="primary" style="width: 350px;" @click.prevent="handlerLogin">登录</el-button>
          </el-form-item>

        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { validMobile } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    // 验证手机号
    const validateMobile = (rules, value, callback) => {
      if (value === '') {
        callback('请输入手机号')
      } else if (!validMobile(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        mobile: '13800000002',
        password: '929itheima.CN032@.20251017'
        /* isAgree: false */
      },
      loginRules: {
        mobile: [
          // { required: true, message: '请输入手机号', trigger: 'blur' },
          // { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: ['blur', 'change'] }
          { validator: validateMobile, trigger: ['blur', 'change'] }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: ['blur', 'change'] }
        ]
        /* isAgree: [
          // required 只能检测 null、undefined、'' 这三种情况
          { validator: (rule, value, callback) => {
           // rule 是校验规则
           // value 是校验值
           // callback 是回调函数，用于返回校验结果

            if (!value) {
              callback(new Error('请同意用户平台使用协议')) // 校验失败
            } else {
              callback()  // 校验通过
            }
          }, trigger: 'change' }
        ] */
      }
    }
  },
  methods: {
    handlerLogin() {
      // 点击登录按钮校验表单
      this.$refs.form.validate(async(valid) => {
        if (!valid) return

        try {
          // 通过校验后发送请求
          const res = await login(this.loginForm)
          console.log('@@', res)
        } catch (error) {
          console.log(error)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38,72,176) url(../../assets/common/login_back.png) no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;

    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center / contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }

    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }

  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;

    .el-card {
      border: none;
      padding: 0;
    }

    h1 {
      padding-left: 35px;
      font-size: 24px;
    }

    .el-input {
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }

    .el-checkbox{
      color: #606266;
    }
  }
}
</style>
